/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.avro.ui.preferences.pages.general;

import static de.thwildau.tm.moses.avro.ui.preferences.PreferenceConstants.PREF_KEY_COMPILE_CPP;
import static de.thwildau.tm.moses.avro.ui.preferences.PreferenceConstants.PREF_KEY_COMPILE_JAVA;
import static de.thwildau.tm.moses.avro.ui.preferences.PreferenceConstants.PREF_KEY_CREATE_SUBDIR_FOR_CPP_NAMESPACE;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.xtext.ui.editor.preferences.fields.LabelFieldEditor;

import de.thwildau.tm.moses.avro.ui.Activator;

/**
 * Preference page for specific configuration of the Code Generator.
 * <p>
 * This page allows to configure whether the Imports of the generated files should be organized (Cleanup
 * Method - Organize Imports).
 * </p>
 * 
 * @author Thomas Kistel
 * @version $Revision: 250 $
 */
public class IdlCompilerPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage,
        IWorkbenchPropertyPage {
    
    private BooleanFieldEditor compileCppEditor;
    private BooleanFieldEditor cppNamespacesEditor;
    private BooleanFieldEditor compileJavaEditor;
    
    public IdlCompilerPreferencePage() {
        super(GRID);
    }

    @Override
    public IAdaptable getElement() {
        return null;
    }

    @Override
    public void setElement(IAdaptable element) {
    }

    @Override
    public void init(IWorkbench workbench) {
    }

    @Override
    protected void createFieldEditors() {
        compileJavaEditor = new BooleanFieldEditor(PREF_KEY_COMPILE_JAVA, "Generate Java Code", getFieldEditorParent()); 
        LabelFieldEditor label = new LabelFieldEditor("If C++ is activated, only the Types of the protocol will be generated (for Java and C++).", getFieldEditorParent());
        compileCppEditor = new BooleanFieldEditor(PREF_KEY_COMPILE_CPP, "Generate C++ Code", getFieldEditorParent());
        cppNamespacesEditor = new BooleanFieldEditor(PREF_KEY_CREATE_SUBDIR_FOR_CPP_NAMESPACE, "Place C++ files into the subdirectory of the namespace.", getFieldEditorParent());
        
        addField(compileJavaEditor);
        addField(label);
        addField(compileCppEditor);
        addField(cppNamespacesEditor);
    }
    
    @Override
    public void propertyChange(PropertyChangeEvent event) {
        super.propertyChange(event);
        if(event.getSource().equals(compileCppEditor) && event.getNewValue() != null) {
            boolean enabled = (Boolean) event.getNewValue();
            cppNamespacesEditor.setEnabled(enabled, getFieldEditorParent());
        }
    }

    protected IPreferenceStore doGetPreferenceStore() {
        return Activator.getInstance().getPreferenceStore();
    }
}
